# -*- coding: utf-8 -*-
"""
Created on Tue Oct 17 11:17:13 2023

@author: xtp

"""

from smalltools import Files, Table
import pandas as pd
import warnings
import math

warnings.filterwarnings("ignore")

path_processing = 'D:/Data/2023-Herbicide/Processing/'
ext_processing = ['*.txt']
path_result = 'D:/Data/2023-Herbicide/Result/'
ext_result = ['*.csv']
Files.mkdirPath(path_result)


def readFile2Table(path, ext, header=None, index_col=None, sep='', skiprows=[], 
                   feature_name='', dropindex=False):
    '''
    批量读取文件至 Pandas表格

    Parameters
    ----------
    path : TYPE
        DESCRIPTION.
    ext : TYPE
        DESCRIPTION.
    header : TYPE, optional
        DESCRIPTION. The default is None.
    index_col : TYPE, optional
        DESCRIPTION. The default is None.
    sep : TYPE, optional
        DESCRIPTION. The default is ''.
    skiprows : TYPE, optional
        DESCRIPTION. The default is [].
    feature_name : TYPE, optional
        DESCRIPTION. The default is ''.
    dropindex : TYPE, optional
        DESCRIPTION. The default is False.

    Returns
    -------
    df_table : TYPE
        DESCRIPTION.

    '''
    process = Files(path, ext)
    df_frame = []
    for path, name in zip(process.filesWithPath, process.filesNoExt): 
        df = pd.read_table(path, header=header, index_col=index_col,
                           sep=sep, skiprows=skiprows)
        df.index.name = feature_name
        df = df.astype(float).T
        df = Table.addIndex(df, index=name, class_name='Filename', drop=dropindex)
        df_frame.append(df)
    df_table = pd.concat(df_frame, axis=0, ignore_index=False)
    return df_table


spectrum = readFile2Table(path_processing, ext_processing, index_col=0, sep='\t',
                          feature_name='WavList', dropindex=True)

def multiIndexExtract(df, slices = [], names = []):
    '''
    表格索引拆分变多级索引

    Parameters
    ----------
    df : TYPE
        DESCRIPTION.
    names : TYPE, optional
        DESCRIPTION. The default is [].

    Returns
    -------
    newindex : TYPE
        DESCRIPTION.

    '''
    multindex_list = []
    index_frame = []
    for i in range(len(list(df.index))):
        for j in slices:
            a=list(df.index)[i]
            try:
                index = list(df.index)[i][j[0]:j[1]]
            except IndexError:
                index = list(df.index)[i][j[0]:]
            multindex_list.append(index)
        multindex = tuple(multindex_list)
        multindex_list = []
        index_frame.append(multindex)
    newindex = pd.MultiIndex.from_tuples(index_frame, names = names)
    df = df.set_index(newindex)
    return df


spectrum_tab = multiIndexExtract(spectrum,
                    slices = [[0,7], [1,2], [3,5], [6,7], [-4]],
        names = ['Filename','Concentration','Varieties','Repetition','Order'])  
spectrum_tab.sort_index(axis=0, level=4, inplace=True)


def factorIterate(df, factor='', features=[], path=''):
    '''
    分析指定因素的指定特征，因素按组迭代，分别生成表格并按因素名保存

    Parameters
    ----------
    factor : str
        指定因素的名称
    features : list[str]
        指定特征的名称
    path : str
        默认保存路径

    Returns
    -------
    None.

    '''
    name_list = []
    iterate_list = []
    if features != []:
        df = df.loc[:, features]
    for name, group in df.groupby(factor):
        name_list.append(name)
        iterate_list.append(group)
        if path != '':
            group.to_csv(path + str(name) + '.csv')
    return name_list, iterate_list

_, df_frame = factorIterate(spectrum_tab, factor = 'Filename')



def cut_df(df, n):
    '''
    将 pandas表格按行平均分为 n份

    Parameters
    ----------
    df : TYPE
        DESCRIPTION.
    n : TYPE
        DESCRIPTION.

    Returns
    -------
    df_tem_list : TYPE
        DESCRIPTION.

    '''
    df_tem_list = []
    df_num = len(df)
    every_epoch_num = math.floor((df_num/n))
    for index in range(n):
        if index < n-1:
            df_tem = df[every_epoch_num * index: every_epoch_num * (index + 1)]
        else:
            df_tem = df[every_epoch_num * index:]
        df_tem_list.append(df_tem)
    return df_tem_list
        
        
df_list_frame = []
for df in df_frame:
    df_list = cut_df(df, 4)
    df_list_frame.extend(df_list)
    
    
df_list_frame_numbered = []
for i, df in enumerate(df_list_frame):
    df_numbered = Table.addIndex(df, index=i, class_name='Leaf', drop=False)
    df_list_frame_numbered.append(df_numbered)
df_merged = pd.concat(df_list_frame_numbered, axis=0)
df_merged.reset_index(level='Order', drop=True, inplace=True)


leaf_spectra_tab = df_merged.groupby(['Filename','Concentration',
                                      'Varieties','Repetition','Leaf']).mean()
leaf_spectra_tab.reset_index(level='Leaf', drop=True, inplace=True)
leaf_spectra_tab = leaf_spectra_tab.div(100)
Table.write(leaf_spectra_tab, path=path_result + 'Spectra.csv')








